//
//  SpinMutex.cpp
//  DemoV2
//
//  Created by 董一龙 on 2022/8/8.
//

#include "SpinMutex.hpp"
thread_local int32_t SpinMutex::times = 0; ///< 自旋计数
void SpinMutex::lock() {
    while(flag.test_and_set(std::memory_order_acquire)) {
        ++times;
        if(times >= threshold) {
            std::this_thread::yield();
        }
    }
    times = std::max(times - threshold, 0);
}

void SpinMutex::unlock() {
    flag.clear(std::memory_order_release);
}
